//
//  AFNetworkToll.m
//  Test
//
//  Created by 张琦 on 15/11/6.
//  Copyright © 2015年 张琦. All rights reserved.
//

#import "AFNetworkToll.h"
#import "AFNetworking.h"
@implementation AFNetworkToll
+ (void)getUrl:(NSString *)url
          body:(id)body
      response:(ZHResponseStyle)style
requestheadFile:(NSDictionary *)headFile
       success:(void (^)(NSURLSessionDataTask *task, id result))success
       failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
    // 1.获取单利的网络管理对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2.根据style的类型，去选择返回值类型
    switch (style) {
        case ZHResponseStyleJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case ZHResponseStyleXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case ZHResponseStyleData:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
    }
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript", nil]];
    if (headFile) {
        for (NSString *key in headFile.allKeys) {
            [manager.requestSerializer setValue:headFile[key] forHTTPHeaderField:key];
        }
    }
    // 5.UTF8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    // 6.发送get请求
    [manager GET:url parameters:body success:^(NSURLSessionDataTask *task, id responseObject) {
        // 防止为空的情况出现
        if (success) {
            success(task, responseObject);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (failure) {
            failure(task, error);
        }
    }];
    
}
@end





